import {
    APIAddReward,
    APIAddActivity,
    APIAddNotice,
    APIDeleteActivity,
    APIDeleteNotice,
    APIDeleteReward, APILogin,
    APIQueryActivity,
    APIQueryActivityCnt,
    APIQueryNotice,
    APIQueryNoticeCnt,
    APIQueryReward,
    APIQueryRewardCnt, APIRegister,
    APIQueryGMList
} from "./const.js";
import {convertLocalDateToUTCSeconds} from "./util.js";

const Errors = {
    NetworkError: 'Network response was not ok',
    FetchingError: 'Error fetching data:',
}

// 登录
export function login(username, password) {
    let reqBody = {
        "username": username,
        "password": password,
    }
    return fetch(APILogin, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response;
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 注册
export function register(username, password) {
    let reqBody = {
        "username": username,
        "password": password,
    }
    return fetch(APIRegister, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response;
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 管理员
export function queryGMList() {
    return fetch(APIQueryGMList, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['gms'];
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 奖励: 增
export function addReward(gm, player, reward, datetime) {
    // 将UTC时间转换为秒数
    let utcRewardSeconds = convertLocalDateToUTCSeconds(datetime);

    let data = {
        gm: gm,
        player: player,
        reward: reward,
        datetime: utcRewardSeconds,
    }
    return fetch(APIAddReward, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 奖励: 删
export function deleteReward(id) {
    let reqBody = {
        "id": id,
    }
    return fetch(APIDeleteReward, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response;
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 奖励: 查总数量
export function queryRewardCnt() {
    return fetch(APIQueryRewardCnt, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['cnt'];
    }).catch(error => {
        console.error(Errors.FetchingError, error);
    })
}

// 奖励: 查指定页数据
export function queryReward(page, pageCapacity) {
    let reqBody = {
        "page": page,
        "page_capacity": pageCapacity,
    }
    return fetch(APIQueryReward, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['rewards'];
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 活动: 增
export function addActivity(gm, activityID, activityName, startTime, endTime) {
    // 将UTC时间转换为秒数
    let utcStartSeconds = convertLocalDateToUTCSeconds(startTime);
    let utcEndSeconds = convertLocalDateToUTCSeconds(endTime);

    let data = {
        gm: gm,
        activity_id: parseInt(activityID),
        activity_name: activityName,
        start_time: utcStartSeconds, // 将UTC秒传递给addActivity函数
        end_time: utcEndSeconds // 将UTC秒传递给addActivity函数
    };
    return fetch(APIAddActivity, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 活动: 删
export function deleteActivity(id) {
    let reqBody = {
        "id": id,
    }
    return fetch(APIDeleteActivity, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response;
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 活动: 查总数量
export function queryActivityCnt() {
    return fetch(APIQueryActivityCnt, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['cnt'];
    }).catch(error => {
        console.error(Errors.FetchingError, error);
    })
}

// 活动: 查指定页数据
export function queryActivity(page, pageCapacity) {
    let reqBody = {
        "page": page,
        "page_capacity": pageCapacity,
    }
    return fetch(APIQueryActivity, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['activities'];
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 公告: 增
export function addNotice(gm, sendTime, exitTime, content) {
    // 将UTC时间转换为秒数
    let utcSendSeconds = convertLocalDateToUTCSeconds(sendTime);
    let utcExitSeconds = convertLocalDateToUTCSeconds(exitTime);

    let data = {
        gm: gm,
        send_time: utcSendSeconds, // 将UTC秒传递给addNotice函数
        exit_time: utcExitSeconds, // 将UTC秒传递给addNotice函数
        content: content,
    };
    return fetch(APIAddNotice, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 公告: 删
export function deleteNotice(id) {
    let reqBody = {
        "id": id,
    }
    return fetch(APIDeleteNotice, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response;
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}

// 公告: 查总数量
export function queryNoticeCnt() {
    return fetch(APIQueryNoticeCnt, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['cnt'];
    }).catch(error => {
        console.error(Errors.FetchingError, error);
    })
}

// 公告: 查指定页数据
export function queryNotice(page, pageCapacity) {
    let reqBody = {
        "page": page,
        "page_capacity": pageCapacity,
    }
    return fetch(APIQueryNotice, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(reqBody),
    }).then(response => {
        if (!response.ok) {
            throw new Error(Errors.NetworkError);
        }
        return response.json();
    }).then(data => {
        return data['notices'];
    }).catch(error => {
        console.log(Errors.FetchingError, error);
    })
}